package com.yanfan.data.controller;

import com.yanfan.common.annotation.Log;
import com.yanfan.common.core.controller.BaseController;
import com.yanfan.common.core.domain.AjaxResult;
import com.yanfan.common.core.page.TableDataInfo;
import com.yanfan.common.enums.BusinessType;
import com.yanfan.common.utils.poi.ExcelUtil;
import com.yanfan.data.domain.Snmp;
import com.yanfan.data.service.SnmpService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/iot/snmp")
public class SnmpController extends BaseController {

    @Autowired
    private SnmpService snmpService;

    /**
     * 查询snmp协议主表列表
     */
//    //@PreAuthorize("@ss.hasPermi('snmp:snmp:list')")
    @GetMapping("/list")
    public TableDataInfo list(Snmp snmp) {
        startPage();
        List<Snmp> list = snmpService.selectSnmpList(snmp);
        return getDataTable(list);
    }

    /**
     * 导出snmp协议主表列表
     */
//    //@PreAuthorize("@ss.hasPermi('snmp:snmp:export')")
    @Log(title = "snmp协议主表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Snmp snmp) {
        List<Snmp> list = snmpService.selectSnmpList(snmp);
        ExcelUtil<Snmp> util = new ExcelUtil<Snmp>(Snmp.class);
        util.exportExcel(response, list, "snmp协议主表数据");
    }

    /**
     * 获取snmp协议主表详细信息
     */
//    //@PreAuthorize("@ss.hasPermi('snmp:snmp:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(snmpService.selectSnmpById(id));
    }

    /**
     * 新增snmp协议主表
     */
//    //@PreAuthorize("@ss.hasPermi('snmp:snmp:add')")
    @Log(title = "snmp协议主表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Snmp snmp) {
        Snmp snmp1 = snmpService.selectSnmpBySnmpIpAddress(snmp.getIpAddress());
        if (null != snmp1) {
            return error("新增失败，设备ip已存在");
        }
        return toAjax(snmpService.insertSnmp(snmp));
    }

    /**
     * 修改snmp协议主表
     */
//    //@PreAuthorize("@ss.hasPermi('snmp:snmp:edit')")
    @Log(title = "snmp协议主表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Snmp snmp) {
        return toAjax(snmpService.updateSnmp(snmp));
    }

    /**
     * 删除snmp协议主表
     */
//    //@PreAuthorize("@ss.hasPermi('snmp:snmp:remove')")
    @Log(title = "snmp协议主表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(snmpService.deleteSnmpByIds(ids));
    }

    @GetMapping(value = "snmpList/get")
    @ApiOperation(value = "SNMP列表读取", httpMethod = "GET", response = AjaxResult.class, notes = "SNMP列表读取")
    public AjaxResult snmpListGet() {
        snmpService.fetchsnmpListGet();
        return AjaxResult.success();
    }
}
